package com.xxkj.drugtraceability.ui.update

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Handler
import android.os.Message
import android.view.View
import com.xxkj.drugtraceability.R
import com.xxkj.drugtraceability.base.BaseVMActivity
import com.xxkj.drugtraceability.databinding.ActivityDoctoradvciebackBinding
import com.xxkj.drugtraceability.databinding.ActivityUpdateBinding
import com.xxkj.drugtraceability.utils.toast
import org.koin.androidx.viewmodel.ext.android.viewModel

/**
 * author: zhoujingjin
 * created on: 2024/9/20 16:20
 * description:
 */
 class UpdateActivity :BaseVMActivity(){
    private val binding by binding<ActivityUpdateBinding>(R.layout.activity_update)
   private val viewModel by viewModel<UpdateViewModel>()
   private var isDownLoad = true
   private val handler: Handler = @SuppressLint("HandlerLeak")
   object : Handler() {
      override fun handleMessage(msg: Message) {
         super.handleMessage(msg)
         when (msg.arg1) {
            0 -> {
               isDownLoad = false
               val intent = msg.obj as Intent
               startActivity(intent)
               finish()
            }
         }
      }
   }

    override fun initView() {
        binding.apply {
            progressBar1.visibility = View.VISIBLE
        }
    }

    override fun initData() {
      val fileName = intent.getStringExtra("fileName")
      val fileUrl = intent.getStringExtra("fileUrl")
       viewModel.download(fileUrl,fileName,handler)
    }

    override fun startObserve() {
    }
   override fun onBackPressed() {
      updateExit()
   }

   /**
    * 下载过程中不允许点击返回键
    */
   private fun updateExit() {
      if (isDownLoad) {
         toast(R.string.not_cancel)
      } else {
         finish()
      }
   }
}